package com.magicborrow.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.beans.BaseBean;
import com.magicborrow.beans.NormalBean1;
import com.magicborrow.beans.UserBean;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.ViewPressedUtil;
import com.magicborrow.utils.VolleyTool;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ghost on 16/6/22.
 */
public class CheckPhoneActivity extends BaseActivity implements View.OnClickListener {
    private ImageView ivBack;
    private TextView tvTitle;
    private TextView tvSend;
    private LinearLayout llFrame;
    private TextView tvAgain;
    private TextView tvHelp;
    private TextView tvConfirm;
    private String phone;
    private String checkCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_phone_activity);

        ivBack = (ImageView) findViewById(R.id.iv_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvSend = (TextView) findViewById(R.id.tv_send);
        llFrame = (LinearLayout) findViewById(R.id.ll_frame);
        tvAgain = (TextView) findViewById(R.id.tv_again);
        tvHelp = (TextView) findViewById(R.id.tv_help);
        tvConfirm = (TextView) findViewById(R.id.tv_confirm);
        tvConfirm.setOnClickListener(this);
        tvConfirm.setOnTouchListener(ViewPressedUtil.TouchPress);
        tvSend.setOnClickListener(this);
    }

    private EditText getEditPhone() {
        return (EditText) findViewById(R.id.edit_phone);
    }

    private EditText getEditCode() {
        return (EditText) findViewById(R.id.edit_code);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_send:
                if(!checkPhone()){return;}
                Map<String,String> params = new HashMap<>();
                params.put("mobile",phone);
                params.put("type","0");
                params.put("channel","1");
                VolleyTool.get(Constants.GET_CHECK_CODE, params, new VolleyTool.HTTPListener() {
                    @Override
                    public <T> void onResponse(T t, int requestCode) {
                        NormalBean1 bean = (NormalBean1)t;
                        if(bean.getCode() == 0){
                            Toast.makeText(CheckPhoneActivity.this,"短信已发送到您的手机,请查收",Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(CheckPhoneActivity.this,bean.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error, int requestCode) {
                        //Toast.makeText(CheckPhoneActivity.this,"服务器忙,请稍后重试",Toast.LENGTH_SHORT).show();
                    }
                }, 1, NormalBean1.class);


                break;
            case R.id.tv_confirm:
                if(!checkPhone()){return;}
                if(!checkCode())return;
                HashMap<String,String> param = new HashMap();
                param.put("acc_token", UserTools.getUser(this).getAcc_token());
                param.put("mobile",phone);
                param.put("code",checkCode);
                VolleyTool.get(Constants.CHECK_UPDATE_MOBILE, param, new VolleyTool.HTTPListener() {
                    @Override
                    public <T> void onResponse(T t, int requestCode) {
                        BaseBean bean = (BaseBean) t;
                        if(bean.getCode()>=0){
                            UserBean.User user = UserTools.getUser(CheckPhoneActivity.this);
                            user.setMobile(phone);
                            UserTools.saveUser(CheckPhoneActivity.this,user);
                            finish();

                        }else {
                            showShortMsg(bean.getMessage());
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error, int requestCode) {

                    }
                },0, BaseBean.class);
                break;
        }
    }
    private boolean checkPhone(){
        phone = getEditPhone().getText().toString();
        if("".equals(phone)){
            Toast.makeText(this,"手机号不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(phone.length() < 11){
            Toast.makeText(this,"请正确填写手机号码",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private boolean checkCode(){
        checkCode = getEditCode().getText().toString();
        if("".equals(checkCode)){
            Toast.makeText(this,"验证码不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
